package com.estronger.shopmanage.view;

import android.app.Activity;
import android.content.Intent;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.tasking.SelectPepoleDeliverListAdpter;
import com.estronger.shopmanage.adapter.tasking.SelectPepoleListAdpter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B11Designer;
import com.estronger.shopmanage.bean.Pepole;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.utils.PrefUtils;
import com.estronger.shopmanage.utils.Toast;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SelectPeopleActivity extends BaseActivity implements View.OnClickListener {

    private View rl_root;
    private Activity mActivity;
    private TextView tvtitle;
    private ImageView imclose;
    private ListView ltpepole;
    private LinearLayout llSureChecked;//多选的选中确认
    private SelectPepoleListAdpter adpter;
    private SelectPepoleDeliverListAdpter adpterDeliver;
    private ArrayList<Pepole.Datas> dataDeliver;
    //1-测量 2-送货 3-安装
    private int whichModule;
    private boolean ifMultiple = false;
    private OnSelectedListener selectedListener = null;

    @Override
    protected void init() {
        mActivity = this;
        mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
        rl_root = View.inflate(mActivity, R.layout.activity_select_people, null);
        mActivity.setContentView(rl_root);
        mActivity.setFinishOnTouchOutside(false);
        whichModule = mActivity.getIntent().getIntExtra(WHITCHMODULE, 0);
        ifMultiple = mActivity.getIntent().getBooleanExtra(IFMULTIPLE, false);
        //设置Theme.Dialog View高度
        WindowManager m = mActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = mActivity.getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.85); // 宽度设置为屏幕的0.7
        mActivity.getWindow().setAttributes(p);
    }

    @Override
    protected void initView() {
        tvtitle = (TextView) mActivity.findViewById(R.id.tvtitle);
        imclose = (ImageView) mActivity.findViewById(R.id.imclose);
        ltpepole = (ListView) mActivity.findViewById(R.id.ltpepole);
        llSureChecked = mActivity.findViewById(R.id.llSureChecked);
    }

    @Override
    protected void initAction() {
        imclose.setOnClickListener(this);
        llSureChecked.setOnClickListener(this);
        selectedListener = new OnSelectedListener() {
            @Override
            public void onSelect(String code, String desc) {
                if(!ifMultiple) {
                    MyApplication.selectID = code;
                    MyApplication.selectPeople = desc;
                    mActivity.finish();
                }
            }
        };
        switch (whichModule) {
            case 1:
                String measureid = mActivity.getIntent().getStringExtra(TASKID);
                RequestParams params = new RequestParams();
                params.addBodyParameter("sf_action", "geteditdesignerdata");
                params.addBodyParameter("usercode", MyApplication.getUserCode());
                params.addBodyParameter("measureid", measureid);
                connect(params, NetManager.PG_CHOISE_DESIGNER);
                tvtitle.setText("选择设计师");
                break;
            case 2:
                RequestParams params1 = new RequestParams();
                params1.addBodyParameter("sf_action", "getdlvmanlist");
                params1.addBodyParameter("usercode", MyApplication.getUserCode());
                connect(params1, NetManager.DELIVER_MENBER);
                tvtitle.setText("选择送货员");
                break;
            case 3:
                String installid = mActivity.getIntent().getStringExtra(TASKID);
                RequestParams params2 = new RequestParams();
                params2.addBodyParameter("sf_action", "getistman");
                params2.addBodyParameter("usercode", MyApplication.getUserCode());
                params2.addBodyParameter("measureid", installid);
                connect(params2, NetManager.PG_CHOISE_INSTALLER);
                tvtitle.setText("选择安装师");
                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imclose:
                mActivity.finish();
                break;
            case R.id.llSureChecked:
                if(adpter != null) {
                    if(adpter.getChecked()) {
                        mActivity.finish();
                    }
                }else if (adpterDeliver != null) {
                   if(adpterDeliver.getChecked()) {
                       mActivity.finish();
                   }
                }
                break;
        }
    }

    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
            if (NetManager.PG_CHOISE_DESIGNER == code) {
                B11Designer dBean = gson.fromJson(result, B11Designer.class);
                if (dBean.success && dBean.data != null) {
                    B11Designer.DesignerData designerInfo = dBean.data;
                    ArrayList<B11Designer.Designer> designers = designerInfo.designers;
                    if (designers == null || designers.size() == 0) {
                        Toast.show(mActivity, "当前没有可供选择的设计师！", Toast.LENGTH_SHORT);
                        mActivity.finish();
                    } else {
                        adpter = new SelectPepoleListAdpter(mActivity, designers, ifMultiple, selectedListener);
                        ltpepole.setAdapter(adpter);
                        setListViewHeight(adpter);
                    }
                } else {
                    Toast.show(mActivity, dBean.msg, Toast.LENGTH_SHORT);
                }
            } else if (code == NetManager.DELIVER_MENBER) {
                Pepole p = gson.fromJson(result, Pepole.class);
                if (p.success) {
                    dataDeliver = p.data;
                    if (dataDeliver == null || dataDeliver.size() == 0) {
                        Toast.show(mActivity, "当前没有可供选择的送货员！", Toast.LENGTH_SHORT);
                        mActivity.finish();
                    } else {
                        adpterDeliver = new SelectPepoleDeliverListAdpter(mActivity, dataDeliver, ifMultiple, selectedListener);
                        ltpepole.setAdapter(adpterDeliver);
                        setListViewHeight(adpterDeliver);
                    }
                } else {
                    Toast.show(mActivity, p.msg, Toast.LENGTH_SHORT);
                }
            } else if (code == NetManager.PG_CHOISE_INSTALLER) {
                Pepole dBean = gson.fromJson(result, Pepole.class);
                if (dBean.success && dBean.data != null) {
                    ArrayList<Pepole.Datas> pDatas = dBean.data;
                    if (pDatas == null || pDatas.size() == 0) {
                        Toast.show(mActivity, "当前没有可供选择的安装师！", Toast.LENGTH_SHORT);
                        mActivity.finish();
                    } else {
                        adpterDeliver = new SelectPepoleDeliverListAdpter(mActivity, pDatas, ifMultiple, selectedListener);
                        ltpepole.setAdapter(adpterDeliver);
                        setListViewHeight(adpterDeliver);
                    }
                } else {
                    Toast.show(mActivity, dBean.msg, Toast.LENGTH_SHORT);
                }
            }
    }

    @Override
    protected void onNetErorr(int code, String msg) {
        super.onNetErorr(code, msg);
    }

    //设置listview的高度
    private void setListViewHeight(Adapter mAdapter) {
        View itemView = mAdapter.getView(0, null, ltpepole); //获取其中的一项
        //进行这一项的测量，为什么加这一步，具体分析可以参考 https://www.jianshu.com/p/dbd6afb2c890这篇文章
        itemView.measure(0, 0);
        int itemHeight = itemView.getMeasuredHeight(); //一项的高度
        int itemCount = mAdapter.getCount();//得到总的项数
        LinearLayout.LayoutParams layoutParams = null; //进行布局参数的设置
        if (itemCount <= 11) {
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight * itemCount);
        } else if (itemCount > 11) {
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight * 11);
        }
        ltpepole.setLayoutParams(layoutParams);
    }

    /**
     * 打开弹框
     * @param activity
     * @param module 模块类型 1-测量 2-送货 3-安装
     * @param taskId  任务id(测量或安装)
     * @param ifMultiple 是否多选
     */
    private static final String WHITCHMODULE = "WhitchModule";//哪个模块 //1-测量 2-送货 3-安装
    private static final String TASKID = "taskid";//哪个模块 //1-测量 2-送货 3-安装
    private static final String IFMULTIPLE = "ifMultiple";
    public static void open(Activity activity, int module, String taskId, boolean ifMultiple) {
        Intent intent = new Intent(activity, SelectPeopleActivity.class);
        intent.putExtra(WHITCHMODULE, module);
        if(taskId != null) {
            intent.putExtra(TASKID, taskId);
        }
        intent.putExtra(IFMULTIPLE, ifMultiple);
        activity.startActivity(intent);
    }

    /**
     * 选中监听
     */
    public static interface OnSelectedListener {
        void onSelect(String code, String desc);
    }
}
